package com.dingli.shop.controller;

import com.dingli.shop.biz.GoodsBiz;
import com.dingli.shop.po.GoodListVO;
import com.dingli.shop.po.Goods;
import com.dingli.shop.vo.JsonVo;
import com.dingli.shop.vo.Meta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
public class GetGoodsController {
    @Autowired
    private GoodsBiz goodsBiz;
//    获取商品列表
    @GetMapping("/goods")
    public @ResponseBody
    JsonVo getGoods(
            @RequestParam Integer pagenum,// 1
            @RequestParam Integer pagesize// 10
            ){
        int fromID,toID;
        toID = pagesize;
        fromID = (pagenum-1)*pagesize;
        Map<String,Integer> map = new HashMap<>();
        map.put("fromID",fromID);
        map.put("toID",toID);
        //获取商品列表
        List<Goods> goods = goodsBiz.getGoodList(map);
        int total = goodsBiz.getCount();
        GoodListVO goodListVO = new GoodListVO();
        goodListVO.setPagenum(pagenum);
        goodListVO.setTotal(total);
        goodListVO.setGoods(goods);
        Meta meta = new Meta();
        if (goods == null) {
            meta.setMsg("没有找到商品");
            meta.setStatus(400);
        } else {
            meta.setMsg("获取商品列表成功");
            meta.setStatus(200);
        }
        JsonVo jsonVo = new JsonVo();
        jsonVo.setMeta(meta);
        jsonVo.setData(goodListVO);
        return jsonVo;
    }
//    删除商品
    @DeleteMapping("delGoods")
    public @ResponseBody
    JsonVo delGoods(
            @RequestParam int delid
    ){
        goodsBiz.delGoodsBuyID(delid);
        JsonVo jsonVo = new JsonVo();
        Meta meta = new Meta();
        meta.setStatus(200);
        meta.setMsg("删除商品成功");
        jsonVo.setMeta(meta);
        return jsonVo;
    }

}
